package luccy.desc.impl;

import java.lang.reflect.Method;

import luccy.Constants;
import luccy.desc.ClassDesc;
import luccy.desc.PropertyDesc;
import luccy.util.MethodUtil;

public class PropertyDescImpl implements PropertyDesc {

	private Method readMethod;

	private Method writeMethod;

	private String propertyName;

	private Class propertyType;

	private ClassDesc classDesc;

	public PropertyDescImpl(ClassDesc classDesc, String propertyName) {
		this.propertyName = propertyName;
		this.classDesc = classDesc;
	}

	public void setReadMethod(Method readMethod) {
		this.readMethod = readMethod;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	public void setWriteMethod(Method writeMethod) {
		this.writeMethod = writeMethod;
	}

	public void setPropertyType(Class propertyType) {
		this.propertyType = propertyType;
	}

	public String getPropertyName() {
		return propertyName;
	}

	public Class getPropertyType() {
		return propertyType;
	}

	public Method getReadMethod() {
		return readMethod;
	}

	public Method getWriteMethod() {
		return writeMethod;
	}

	public <T> T getValue(T target) {
		return MethodUtil.invoke(readMethod, target, Constants.EMPTY_ARGS);
	}

	public <T> void setValue(T target, Object... value) {
		try {
			MethodUtil.invoke(writeMethod, target, value);
		} catch (Throwable t) {
			throw new IllegalPropertyRuntimeException(classDesc
					.getComponentClass(), propertyName, t);
		}
	}

}
